<html>
    <head>
        <meta charset="utf-8">
        <script src="../dist/claygl.js"></script>
    </head>
    <body style="margin:0px;">
        <canvas width="1200" height="640" id="main"></canvas>
        <script>
            var Shader = clay.Shader;

            var renderer = new clay.Renderer({
                canvas: document.getElementById('main'),
                devicePixelRatio: 1.0
            });
            renderer.resize(window.innerWidth, window.innerHeight);

            var scene = new clay.Scene();
            var camera = new clay.camera.Perspective({
                aspect: renderer.getViewportAspect(),
                far: 500
            });

            var shader = clay.shader.library.get('clay.basic');

            var material = new clay.Material({
                shader: shader
            });
            var texture = new clay.Texture2D({
                wrapS: clay.Texture.REPEAT,
                wrapT: clay.Texture.REPEAT,
                width: 32,
                height: 32,
                mipmaps: [
                    createMipMap(32, '#000'),
                    createMipMap(16, '#222'),
                    createMipMap(8, '#555'),
                    createMipMap(4, '#999'),
                    createMipMap(2, '#aaa'),
                    createMipMap(1, '#fff')
                ]
            });
            var textureCube = new clay.TextureCube({
                wrapS: clay.Texture.REPEAT,
                wrapT: clay.Texture.REPEAT,
                width: 128,
                height: 128,
                mipmaps: [
                    createMipMapCube(128, '#02a'),
                    createMipMapCube(64, '#000'),
                    createMipMapCube(32, '#000'),
                    createMipMapCube(16, '#f00'),
                    createMipMapCube(8, '#f0f'),
                    createMipMapCube(4, '#00f'),
                    createMipMapCube(2, '#0f0'),
                    createMipMapCube(1, '#f00')
                ]
            });

            material.set('diffuseMap', texture);
            material.set('uvRepeat', [100, 100]);

            function createMipMap(size, color) {
                var canvas = document.createElement('canvas');
                canvas.width = canvas.height = size;
                var ctx = canvas.getContext('2d');
                ctx.fillStyle = color;
                ctx.fillRect(0, 0, size, size);
                var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;

                return {
                    pixels: new Uint8Array(imgData)
                };
            }

            function createMipMapCube(size, color) {
                var canvas = document.createElement('canvas');
                canvas.width = canvas.height = size;
                var ctx = canvas.getContext('2d');
                ctx.fillStyle = color;
                ctx.fillRect(0, 0, size, size);
                var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;

                return {
                    pixels: ['px', 'nx', 'py', 'ny', 'pz', 'nz'].reduce(function (obj, target) {
                        obj[target] = new Uint8Array(imgData);
                        return obj;
                    }, {})
                };
            }

            var root = new clay.Node();

            camera.position.set(0, 4, 14);
            camera.lookAt(new clay.Vector3(0, 1, 0));

            scene.add(root);
            // Add Plane
            var plane = new clay.geometry.Plane({
                widthSegments: 1,
                heightSegments: 1
            });
            var planeMesh = new clay.Mesh({
                geometry: plane,
                material: material,
                scale: new clay.Vector3(60, 60, 60)
            });
            planeMesh.position.y = -0.8;
            planeMesh.rotation.rotateX(-Math.PI/2);
            root.add(planeMesh);

            var sphereGeo = new clay.geometry.Sphere();
            var envMapMaterial = new clay.Material({
                shader: clay.shader.library.get('clay.standard')
            });
            envMapMaterial.set('reflectivity', 1.0);
            envMapMaterial.set('environmentMap', textureCube);
            for (var i = 0; i < 10; i++) {
                var sphere = new clay.Mesh({
                    geometry: sphereGeo,
                    material: envMapMaterial
                });
                sphere.scale.set(5, 5, 5);
                sphere.position.set(-10, 5, -i * 10);
                root.add(sphere);
            }

            var timeline =  new clay.Timeline();
            timeline.on('frame', function () {
                renderer.render(scene, camera);
            });
            timeline.start();

        </script>
    </body>
</html>